<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a :class="[obj.sorter==null?'active':'']"  @click="handle(null)" href="javascript:;">推荐</a>
      <a :class="[obj.sorter=='weight_desc'?'active':'']" @click="handle('weight_desc')" href="javascript:;">最新</a>
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <van-pull-refresh @refresh="onRefresh" v-model="refreshing">
      <van-list
        :finished="finished"
        @load="onLoad"
        finished-text="我是有底线的"
        offset="50"
        style="padding-bottom:30px"
        v-model="loading"
      >
        <!-- 遍历组件 -->
        <ArticleItem
          v-for="item in list"
          :key="item.id"
          :item="item"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { ArticleApi } from '../api/article'
export default {
  name: 'article-page',
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      obj: {
        current: 0, //当前页
        sorter: null,
        pageSize: 10//每页数量
      }
    }
  },
  created () {
    // this.getArticleList()
  },
  methods: {
     handle (sorter) {
      this.obj.sorter = sorter
      this.refreshing = true
      this.onRefresh()
      },
      goDetail(id){

console.log(id);
      },

    onLoad () {

      if (this.obj.current > 3) {
        this.loading = false// 结束当前加载
        this.finished = true//结束上拉，设置值为true就表示不能上拉了
        return
      }
      this.obj.current += 1
      this.getArticleList()
      // }, 3000);
    },
    async getArticleList () {
      //   const  res=await ArticleApi(this.obj).catch(err=>err)
      //  if(res.code==10000){
      //   this.list=res.data.rows

      //  }
      try {
        const res = await ArticleApi(this.obj)
        // this.list = res.data.rows
        this.list = this.refreshing ? res.data.rows : [...this.list, ...res.data.rows]
        console.log(this.list);
        this.loading=false
        this.refreshing=false

      } catch (error) {

      }

    },
    onRefresh () {
      /* 下拉就会触发 */
      // 清空列表数据
      this.finished = false; //finished:true  不能开启上拉 表示又可以继续上拉了

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      // this.loading = true;
      this.obj.current = 0
      this.onLoad();
    },

  },
}
</script>

<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
</style>